import { computed, onUnmounted, ref } from 'vue'
import dayjs from 'dayjs'
export const useCountDown = () => {
  // 建立响应式数据
  let dsqtimer = null
  const timer = ref(0)
  // 格式化时间
  const formatTime = computed(() => dayjs.unix(timer.value).format('mm分ss秒'))
  // 开启定时器的函数
  const start = (currtTime) => {
    timer.value = currtTime
    // 开启定时器--
    dsqtimer = setInterval(() => {
      timer.value--
    }, 1000)
  }
  // 组件销毁时清除定时器
  onUnmounted(() => {
    dsqtimer && clearInterval(dsqtimer)
  })
  return {
    start,
    formatTime
  }
}
